#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1

RESOLV_CONF="/etc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"

last_action=`cat /tmp/udhcpc_$interface.action`
echo -n $1 > /tmp/udhcpc_$interface.action;

case "$1" in
	renew|bound)
		last_ip=`cat /tmp/udhcpc_$interface.ip`
		echo -n $ip > /tmp/udhcpc_$interface.ip
		if [ "$last_ip" != "$ip" ]; then
			/sbin/ifconfig $interface $ip $BROADCAST $NETMASK;
			#/* remove all binding entries after getting new IP */
			HWNAT=`nvram_get 2860 hwnatEnabled`
			if [ "$HWNAT" = "1" ]; then
				rmmod hw_nat
				insmod -q hw_nat
			fi

			if [ -n "$router" ] ; then
				echo "deleting routers"
				while route del default gw 0.0.0.0 dev $interface ; do
					:
				done

				metric=0
				for i in $router ; do
					metric=`expr $metric + 1`
					route add default gw $i dev $interface metric $metric
				done
			fi

			echo -n > $RESOLV_CONF
			[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
			for i in $dns ; do
				echo adding dns $i
				echo nameserver $i >> $RESOLV_CONF
			done
			killall dnsmasq;
			ntp.sh
		fi
		;;

	*)
		echo -n > /tmp/udhcpc_$interface.ip
		[ "$last_action" == "renew" ] && killall dnsmasq;
		[ "$last_action" == "bound" ] && killall dnsmasq;
		;;

esac

exit 0

